package it.slothy.noveleditor.interfaces;

import it.slothy.noveleditor.listener.DataChangedEvent;
import it.slothy.noveleditor.listener.DataChangedEventListener;
import it.slothy.noveleditor.listener.FrameCloseEvent;
import it.slothy.noveleditor.listener.FrameCloseEventListener;

import javax.swing.JFrame;
import javax.swing.event.EventListenerList;

public class MEJFrame extends JFrame{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	//Costruttore
	public MEJFrame(){
		super();
	}
	
	protected EventListenerList listenerList = new EventListenerList();

	public void addDataChangeEventListener(DataChangedEventListener listener) {
		listenerList.add(DataChangedEventListener.class, listener);
	}
	
	public void removeDataChangeEventListener(DataChangedEventListener listener) {
		listenerList.remove(DataChangedEventListener.class, listener);
	}
	
	public <T> void addFrameCloseEventListener(FrameCloseEventListener<T> listener) {
		listenerList.add(FrameCloseEventListener.class, listener);
	}
	
	public <T> void removeFrameCloseEventListener(FrameCloseEventListener<T> listener) {
		listenerList.remove(FrameCloseEventListener.class, listener);
	}
	
	@SuppressWarnings("unchecked")
	public <T> void frameClosedEvent(FrameCloseEvent<T> evt) {
		Object[] listeners = listenerList.getListenerList();
		for (int i = 0; i < listeners.length; i = i+2) {
				if (listeners[i] == FrameCloseEventListener.class) {
					((FrameCloseEventListener<T>) listeners[i+1]).frameClosed(evt);
				}
		}
	}
	
	public void fireDataChangeEvent(DataChangedEvent evt) {
		Object[] listeners = listenerList.getListenerList();
		for (int i = 0; i < listeners.length; i = i+2) {
				if (listeners[i] == DataChangedEventListener.class) {
					((DataChangedEventListener) listeners[i+1]).dataChangedOccured(evt);
				}
		}
	}
	
	public void closeFrameEvent(DataChangedEvent evt) {
		Object[] listeners = listenerList.getListenerList();
		for (int i = 0; i < listeners.length; i = i+2) {
				if (listeners[i] == DataChangedEventListener.class) {
					((DataChangedEventListener) listeners[i+1]).frameClosed(evt);
				}
		}
	}

}
